package cn.kmsoft.jssc.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

public class RabbitMqConfig {
    private static final Logger logger = LoggerFactory.getLogger(RabbitMqConfig.class);
    @Autowired
    private Environment env;
    private boolean enviromentInited = false;

    //队列
    //public static final String TOPIC_QUEUE_JSSC = "jssc.kcy.queue";
    //public static final String TOPIC_ROUTINGKEY_JSSC = "jssc.kcy.routingkey";
    //一个交换机
    public static final String TOPIC_EXCHANGE_JSSC = "jssc.kcy.exchange";
    //两个消息队列
    public static final String JAVA2ARCPY_JSSC_QUEUE= "java2arcpy.jssc.queue";//java端向python端发送的消息队列
    public static final String AECPY2JAVA_JSSC_QUEUE = "arcpy2java.jssc.queue";//python端向java端发送的消息队列
    //同一个交换机上的不同消息键，对应路由到不同的消息队列中
    public static final String JAVA2ARCPY_JSSC_ROUTINGKEY= "java2arcpy.jssc.routingkey";//java端向python端发送的路由键
    public static final String AECPY2JAVA_JSSC_ROUTINGKEY = "arcpy2java.jssc.routingkey";//python端向java端发送的路由键

    public  static final int RMQExpirationMillSecond = 1000*60*5;//消息队列过期时间-5分钟

    private String m_mqHost="";
    private int m_mqPort=5672;
    private String m_mqUserName="";
    private String m_mqPassword="";
    private String m_mqVirtualHost="";

    public RabbitMqConfig(){
        //
    }
    public RabbitMqConfig(Environment env){
        this.env=env;
        this.init();
        this.enviromentInited = true;
    }

    private void init() {
        this.m_mqHost = env.getProperty("spring.rabbitmq.host");
        this.m_mqPort = Integer.parseInt(env.getProperty("spring.rabbitmq.port"));
        this.m_mqUserName = env.getProperty("spring.rabbitmq.username");
        this.m_mqPassword = env.getProperty("spring.rabbitmq.password");
        this.m_mqVirtualHost= env.getProperty("spring.rabbitmq.virtual-host");
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        if(enviromentInited==false) {
            this.init();
            this.enviromentInited = true;
        }

        CachingConnectionFactory connectionFactory = new CachingConnectionFactory(m_mqHost);
        connectionFactory.setPort(m_mqPort);
        connectionFactory.setUsername(m_mqUserName);
        connectionFactory.setPassword(m_mqPassword);
        connectionFactory.setVirtualHost(m_mqVirtualHost);

        return connectionFactory;
    }

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory());
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate mqTemplate = new RabbitTemplate(connectionFactory());
        //mqTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        //自定义消息转换器
        mqTemplate.setMessageConverter(new JsscMqMessageConverter());
        return mqTemplate;
    }
//
//    // 配置消费者监听的容器
//    @Bean
//    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory() {
//        SimpleRabbitListenerContainerFactory containerFactory = new SimpleRabbitListenerContainerFactory();
//        containerFactory.setConnectionFactory(connectionFactory());
//        containerFactory.setConcurrentConsumers(3);
//        containerFactory.setMaxConcurrentConsumers(7);
//        //containerFactory.setAcknowledgeMode(AcknowledgeMode.MANUAL);//设置确认模式手工确认
//        //containerFactory.setMessageConverter(new Jackson2JsonMessageConverter());
//        return containerFactory;
//    }
}
